﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ForumDAL
{
    public class SectionDAL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<ForumDomainModels.Section> GetAllSections()
        {
            string connectionString = Properties.Settings.Default.connectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = "SELECT Id, Name FROM Sections";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (!reader.HasRows)
                        {
                            throw new CustomExceptions.NoRowsRetrieved();
                        }
                        else
                        {
                            List<ForumDomainModels.Section> sections = new List<ForumDomainModels.Section>();

                            while (reader.Read())
                            {
                                ForumDomainModels.Mappers.SectionMapper sectionMapper = 
                                    new ForumDomainModels.Mappers.SectionMapper();
                                ForumDomainModels.Section currentSection = sectionMapper.DataFromDatabaseToSectionDomainModel(reader);
                                sections.Add(currentSection);
                            };
                            return sections;
                        }
                    }
                }
            }
        }
    }
}
